define([
	"UtilDir/util",
	"UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
	"static/app/hddc/hddcCjHistorypoints/hddcreplaymap",
	"static/app/hddc/hddccjcommon/hddccjCommon",
	"static/app/hddc/hddccjtask/tmap",
	"Date","DateCN", "css!DateCss",
	"EasyUI","EasyUI-lang"
    ],function(Util, Tool, SelectOrg, SearchBlock,hddcreplaymap,HddccjCommon){

    var sysPath =  getServer() + "/static/app/hddc/hddcCjHistorypoints";

    var hddcCjHistorypointsFormHtml = sysPath + "/views/hddcCjHistorypointsForm.html";
	var getHddcCjHistorypointsController = function(){
		return getServer() + "/hddc/hddcCjHistorypointss";
	};

	/**
	 * 页面初始化
	 */
	var init = function(){
        initSearchBlock();
        queryBtnBind();
		initStartTimeTime();
		initEndTimeTime();
		HddccjCommon.initProjectSelect("projectName"); //添加页面项目名称下拉
		initSingleTaskSelect("projectName", "taskName");
		//addProjectData();
		createHddcCjHistorypointsGrid();
		$("#taskMapLi").css("display", "none");
	};

	var initSingleTaskSelect=function (projectSelectId, taskSelectId) {
		$("#"+projectSelectId).change(function () {
			var html = '<option value="" disabled selected style=\'display:none;\'>请选择</option>';
			var projectName = $("#"+projectSelectId).val();
			if(projectName!=null) {
				var taskdatas = HddccjCommon.getAllTaskByProjectName(projectName);
				$.each(taskdatas, function (idx, item) {
					html += "<option value='" + item.taskId + "'>" + item.taskName + "</option>";
				});
				$("#"+taskSelectId+" option").remove();
				$("#"+taskSelectId).append(html);
			}
		});
	}

	var addProjectData = function () {
		var html = '<option value="" disabled selected style="display:none;">请选择</option>';
		$("#belongProject option").remove();
		$("#belongProject").append(html);
		if(projectData!=null) {
			for (var i = 0; i < projectData.length; i++) {
				html += "<option value='" + projectData[i].projectName + "' exid='" + projectData[i].projectId + "'>" + projectData[i].projectName + "</option>";
			}
		}
		$("#belongProject").append(html);
	}

	$("#taskoperatelab").on('shown.bs.tab', function (e) {
		$("#taskMapLi").css("display", "none");
		hddcreplaymap.initClearMap();
		initSearchBlock();
	});

	$("#taskMaplab").on('shown.bs.tab', function (e) {
		hddcreplaymap.initMap(replayData);
	});

	var initStartTimeTime = function() {
		$("#startTimeCondition").datetimepicker({
			//设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
			language : "cn",
			//输出格式化
			format : 'yyyy-mm-dd hh:ii:ss',
			//直接选择‘今天’
			todayBtn : true,
			//设置最精确的时间选择视图
			minView : 'hour',
			//高亮当天日期
			todayHighlight : true,
			//显示“上午”“下午”
			showMeridian : true,
			//选择完毕后自动关闭
			autoclose : true
		});
	};
	var initEndTimeTime = function() {
		$("#endTimeCondition").datetimepicker({
			//设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
			language : "cn",
			//输出格式化
			format : 'yyyy-mm-dd hh:ii:ss',
			//直接选择‘今天’
			todayBtn : true,
			//设置最精确的时间选择视图
			minView : 'hour',
			//高亮当天日期
			todayHighlight : true,
			//显示“上午”“下午”
			showMeridian : true,
			//选择完毕后自动关闭
			autoclose : true
		});
	};

		var initStartTimeTimeDate = function() {
			$("#startTime").datetimepicker({
				//设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
				language : "cn",
				//输出格式化
				format : 'yyyy-mm-dd hh:ii:ss',
				//直接选择‘今天’
				todayBtn : true,
				//设置最精确的时间选择视图
				minView : 'hour',
				//高亮当天日期
				todayHighlight : true,
				//显示“上午”“下午”
				showMeridian : true,
				//选择完毕后自动关闭
				autoclose : true
			});
		};
		var initEndTimeTimeDate = function() {
			$("#endTime").datetimepicker({
				//设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
				language : "cn",
				//输出格式化
				format : 'yyyy-mm-dd hh:ii:ss',
				//直接选择‘今天’
				todayBtn : true,
				//设置最精确的时间选择视图
				minView : 'hour',
				//高亮当天日期
				todayHighlight : true,
				//显示“上午”“下午”
				showMeridian : true,
				//选择完毕后自动关闭
				autoclose : true
			});
		};

	var initSearchBlock = function(){
        SearchBlock.init("searchBlock");
	};
	var queryBtnBind = function(){
        $("#btnSearch").click(function () {
        	if($("#projectName").val()!=null && $("#taskName").val()==null){
        		// Util.confirm("请选择任务名称后查询！");
        		Util.alert("请选择任务名称!")
			}else{
                createHddcCjHistorypointsGrid();
			}

        });
        $("#btnReset").click(function () {
			$("#startTimeCondition").val("");
				$("#endTimeCondition").val("");
				$("#taskIdCondition").val("");
				// $("#remarkCondition").val("");
				$("#projectName").val("");
            	$("#taskName").val("");
			});
	};

	var replayData=null;
	window.replaydata = function(data){
		console.log(data);
		replayData=data;
		$("#taskMapLi").css("display", "block");
		$('#myTab a[href="#replayMap"]').tab('show');
		//hddcreplaymap.initMap(data);
	}

	var createHddcCjHistorypointsGrid= function() {
		var taskDatas=HddccjCommon.getAllTask();
        $("#hddcCjHistorypointsGrid").datagrid({
            url:getHddcCjHistorypointsController() + "/queryHddcCjHistorypointss",
			method:"GET",
            fitColumns: true,
            autoRowHeight: false,
            columns:[[
                {field:"ck",checkbox:true},
				{
					field:'ddd',
					title:'',
					width:'25%',
					align:'center',
					formatter:function(value,rowData){
						
						return '<a href="#" onclick="replaydata(\'' + rowData.lonlatArray + '\');"> '+'轨迹回放'+' </a> '
					}
				},
				{
					field:'startTime',
					title:'开始时间',
					width:'25%',
					align:'center',
				},
				{
					field:'endTime',
					title:'结束时间',
					width:'25%',
					align:'center',
				},
                // {
                //     field:'remark',
                //     title:'项目名称',
                //     width:'25%',
                //     align:'center',
                //     formatter:function(value,rowData){
                //         debugger
                //         for(var i=0;i<taskDatas.length;i++){
                //             if(rowData.taskId==taskDatas[i].taskId){
                //                 // rowData.remark = rowData.belongProject;
                //                 return taskDatas[i].belongProject;
                //             }
                //         }
                //         return rowData.taskId;
                //     }
                // },
				{
					field:'taskId',
					title:'任务名称',
					width:'25%',
					align:'center',
					formatter:function(value,rowData){
						debugger
						for(var i=0;i<taskDatas.length;i++){
							if(rowData.taskId==taskDatas[i].taskId){
								return taskDatas[i].taskName;
							}
						}
						return rowData.taskId;
					}
				},
            ]],
            toolbar: [
            	/*
            	{
                iconCls: 'fa fa-plus-circle',
                text:"添加",
                handler: function(){
                    addHddcCjHistorypoints();
                }
            },
            */{
                iconCls: 'fa fa-trash-o',
                text:"删除",
                handler: function(){
                    deleteHddcCjHistorypoints();
                }
            }],
            queryParams:{
				startTime: $("#startTimeCondition").val(),
				endTime: $("#endTimeCondition").val(),
				// remark: $("#remarkCondition").val(),
                // projectName:$("#projectName").val(),
                // remark:$("#projectName").val(),
                // taskName:$("#taskName").val(),
				taskId: $("#taskName").val(),
            },
            pagination: true,
            pageSize: 10
        });
    };
	var formValidator = function(){
		$("#hddcCjHistorypointsForm").validate({
			rules : {
				startTime : {
					required : true,
				},
				endTime : {
					required : true,
				},
				taskId : {
					required : true,
				},
				userId : {
					required : true,
				},
			},
			messages : {
				startTime : {
					required : "开始时间不允许为空!",
				},
				endTime : {
					required : "结束时间不允许为空!",
				},
				taskId : {
					required : "任务Id不允许为空!",
				},
				userId : {
					required : "用户Id不允许为空!",
				},
			}
		});
	};
	var getHddcCjHistorypoints = function(id){
		$.ajax({
			url: getHddcCjHistorypointsController() + "/"+id,
			type: "get",
			success: function (data) {
				Tool.deserialize("hddcCjHistorypointsForm", data);
			}
		});
	};

	var addHddcCjHistorypoints = function () {
		var slidebar = Util.slidebar({
			url: hddcCjHistorypointsFormHtml,
			width: "580px",
			cache: false,
			close : true,
			afterLoad: function () {
				initStartTimeTimeDate();
				initEndTimeTimeDate();
				formValidator();
				$("#saveBtn").on("click", function () {
					if($("#hddcCjHistorypointsForm").valid()){
						var data = Tool.serialize("hddcCjHistorypointsForm");
						$.ajax({
							url: getHddcCjHistorypointsController()+"/save" ,
                            contentType:"application/json",
							data: JSON.stringify(data),
							type: "post",
							success: function (data) {
								Util.alert(data.message);
								slidebar.close();
                                createHddcCjHistorypointsGrid();
							}
						});
					}
				});
			}
		});
	};
	window._editHddcCjHistorypoints = function(hddcCjHistorypointsId) {
		var slidebar = Util.slidebar({
			url: hddcCjHistorypointsFormHtml,
			width: "580px",
			cache: false,
			close : true,
			afterLoad: function () {
				initStartTimeTimeDate();
				initEndTimeTimeDate();
				formValidator();
				getHddcCjHistorypoints(hddcCjHistorypointsId);
				$("#saveBtn").on("click", function () {
					if($("#hddcCjHistorypointsForm").valid()){
						var data = Tool.serialize("hddcCjHistorypointsForm");
						$.ajax({
							url: getHddcCjHistorypointsController(),
                            contentType:"application/json",
                            data: JSON.stringify(data),
							type: "post",
							success: function (data) {
								Util.alert(data.message);
								slidebar.close();
								createHddcCjHistorypointsGrid();
							}
						});
					}
				});
			}
		});
	};
	var deleteHddcCjHistorypoints = function() {
		var rows = $("#hddcCjHistorypointsGrid").datagrid("getSelections");
		if (rows == null || rows.length == 0) {
			Util.alert("请选择一行数据!");
			return;
		}
		Util.confirm("是否要删除选中的数据?", function() {
			var ids = "";
			$.each(rows, function(i, row){
				ids += row.uuid + ",";

			});
			ids = ids.substr(0,ids.length - 1);
			$.ajax({
				url: getHddcCjHistorypointsController()+"/delete" ,
                contentType:"application/json",
                data: ids,
				type: "post",
				success: function (data) {
					createHddcCjHistorypointsGrid();
				}
			});
		}, function() {
			return;
		});

	};

	return {
		init:init
	};
});
